----------------------------------------------------------------
-- Global Variables
----------------------------------------------------------------

AuctionHouseWindow = 
{
    DEBUG_ON = false,
    DEBUG_LEVEL = 6,
    SHOW_FAKE_DATA_BUTTON = false,
    
    versionNumber = 0.2,
    
    -- Constants
    TAB_BROWSE_ID   = 1,
    TAB_BIDS_ID     = 2,
    TAB_AUCTIONS_ID = 3,
    numberOfTabs = 3,
    
    -- Strings
    titleBarText = GetStringFromTable( "AuctionHouseStrings",  StringTables.AuctionHouse.MAIN_TITLE ),
    okayButtonLabel = GetStringFromTable( "AuctionHouseStrings",  StringTables.AuctionHouse.MAIN_ERROR_MESSAGE_CONTINUE_BUTTON ),
     
    
    savedSettings = {}
}

----------------------------------------------------------------
-- Local Variables
----------------------------------------------------------------

local windowName = "AuctionHouseWindow"

----------------------------------------------------------------
-- AuctionHouseWindow static data
----------------------------------------------------------------


AuctionHouseWindow.tabWindowNames = 
{
    [AuctionHouseWindow.TAB_BROWSE_ID]     = windowName.."TabsBrowseView",
    [AuctionHouseWindow.TAB_BIDS_ID]       = windowName.."TabsBidsView",
    [AuctionHouseWindow.TAB_AUCTIONS_ID]   = windowName.."TabsAuctionsView",
}

AuctionHouseWindow.tabTitles = 
{
    [AuctionHouseWindow.TAB_BROWSE_ID]     = GetStringFromTable( "AuctionHouseStrings",  StringTables.AuctionHouse.MAIN_TAB_BROWSE ),
    [AuctionHouseWindow.TAB_BIDS_ID]       = GetStringFromTable( "AuctionHouseStrings",  StringTables.AuctionHouse.MAIN_TAB_BIDS ),
    [AuctionHouseWindow.TAB_AUCTIONS_ID]   = GetStringFromTable( "AuctionHouseStrings",  StringTables.AuctionHouse.MAIN_TAB_AUCTIONS ),
}
    
    
local settingsPlaceholder = ""
AuctionHouseWindow.openedWindows = 
{
	[windowName.."CreateAuctionTitle"] = settingsPlaceholder,
	[windowName.."CreateAuction"] = settingsPlaceholder,
	[windowName.."CreateSearchTitle"] = settingsPlaceholder,
	[windowName.."CreateSearch"] = settingsPlaceholder,
	[windowName.."SearchResultsTitle"] = settingsPlaceholder,
	[windowName.."SearchResults"] = settingsPlaceholder,
}


-- TODO: store this on client so we can remember it next time
AuctionHouseWindow.windowSettings = {}

AuctionHouseWindow.windowSettings[windowName.."CreateAuctionTitle"] =
{	
	showCloseButton = false,
	showMinMaxButton = true,
	showOptionsButton = false,

	isMinimized = false,
	--isClosed = false,
	isDocked = true,
	parentWindow = windowName,
	anchorWindow = windowName,
	anchorWindowPoint = "top",			-- attribte point in XML
	anchorPoint = "top",					-- attribte relativePoint in XML
	x="0",
	y="101",
	
	mainWindow = CreateAuctionWindow,
	mainWindowName = windowName.."CreateAuction",
	minimizedWindowName = windowName.."CreateAuctionTitle",
		
	-- WARNING: Need to check that windows anchored outside their parent are still interactable
	-- NOTE: may want to use sticky="true" to keep windows from being pushed offscreen
	
	-- TODO: we may want to allow resizing, though that 
	--	 may automatically be kept in user settings
	-- NOTE: this may require us to use scrollwindows to get to fields at bottom
	--   also probably need a minimum and maximum size
	--   if minimum == maximum then don't allow resize anyway
	
	-- TODO: we may want to allow rotating the orientation to rearrange it's

}


AuctionHouseWindow.windowSettings[windowName.."CreateSearchTitle"] =
{
	showCloseButton = false,
	showMinMaxButton = true,
	showOptionsButton = false,

	isMinimized = false,
	--isClosed = false,
	isDocked = true,
	parentWindow = windowName,
	anchorWindow = windowName.."CreateAuction",
	anchorWindowPoint = "bottom",			-- attribte point in XML
	anchorPoint = "top",					-- attribte relativePoint in XML
	x="0",
	y="0",
	
	mainWindow = AuctionSearchWindow,
	mainWindowName = windowName.."CreateSearch",
	minimizedWindowName = windowName.."CreateSearchTitle",
}

AuctionHouseWindow.windowSettings[windowName.."SearchResultsTitle"] =
{
	showCloseButton = false,
	showMinMaxButton = true,
	showOptionsButton = false,

	isMinimized = false,
	--isClosed = false,
	isDocked = true,
	parentWindow = windowName,
	anchorWindow = windowName.."CreateSearch",
	anchorWindowPoint = "bottom",			-- attribte point in XML
	anchorPoint = "top",					-- attribte relativePoint in XML
	x="0",
	y="0",
	
	mainWindow = AuctionListWindow,
	mainWindowName = windowName.."SearchResults",
	minimizedWindowName = windowName.."SearchResultsTitle",
}



----------------------------------------------------------------
-- Local Functions
----------------------------------------------------------------

local function SendDebug( text, level )

	level = level or 5
	
    if AuctionHouseWindow.DEBUG_ON and level >= AuctionHouseWindow.DEBUG_LEVEL then
        DEBUG( text )
    end
end



----------------------------------------------------------------
-- AuctionHouseWindow Functions
----------------------------------------------------------------


function AuctionHouseWindow.Initialize()
SendDebug(L"AuctionHouseWindow.Initialize")

	AuctionHouseWindow.LoadSettings()
	
	-- initialize all child windows first
	CreateAuctionWindow.Initialize()
	AuctionSearchWindow.Initialize()
	AuctionListDataManager.Initialize()
	AuctionListWindow.Initialize()
	
    WindowRegisterEventHandler( windowName, SystemData.Events.AUCTION_INIT_RECEIVED, "AuctionHouseWindow.Show" )
    WindowRegisterEventHandler( windowName, SystemData.Events.AUCTION_SEARCH_RESULT_RECEIVED, "AuctionHouseWindow.OnSearchResultsReceived" )
	WindowRegisterEventHandler( windowName, SystemData.Events.INTERACT_DONE, "AuctionHouseWindow.Hide" )
	
	VersatileFrame.setMultipleWindowSettings( AuctionHouseWindow.windowSettings )
	--AuctionHouseWindow.Resize()
	
   -- Set all static data    
    LabelSetText( windowName.."TitleBarText", AuctionHouseWindow.titleBarText )

	-- DEBUG
	ButtonSetText( windowName.."FakeDataButton", L"Make Fake Data" )
	WindowSetShowing( windowName.."FakeDataButton", AuctionHouseWindow.SHOW_FAKE_DATA_BUTTON )
	-- END DEBUG

	-- TABS
	local currentTabNumber = AuctionListDataManager.GetCurrentTabNumber()
	for tabNumber = 1, AuctionHouseWindow.numberOfTabs do
		ButtonSetText( AuctionHouseWindow.tabWindowNames[tabNumber], AuctionHouseWindow.tabTitles[tabNumber] )
		AuctionHouseWindow.SetTabShowing( tabNumber, (tabNumber == currentTabNumber) )
	end
end

function AuctionHouseWindow.Shutdown()

	AuctionHouseWindow.Hide()
	AuctionHouseWindow.SaveSettings()
end



function AuctionHouseWindow.OnSearchResultsReceived( searchResultsTable )
SendDebug(L"AuctionHouseWindow.OnSearchResultsReceived")

    -- don't bother updating if window is hidden
    if not AuctionListWindow.IsTitleShowing() then
		return
	end
    
    if not AuctionListWindow.IsShowing() then
		VersatileFrame.Maximize( windowName.."SearchResultsTitle" )
	end
	
	AuctionListWindow.OnSearchResultsReceived( searchResultsTable )
end


function AuctionHouseWindow.Show()
SendDebug(L"AuctionHouseWindow.Show")

    WindowSetShowing( windowName, true )
end

function AuctionHouseWindow.Hide()
SendDebug(L"AuctionHouseWindow.Hide")
    WindowSetShowing( windowName, false )
end

function AuctionHouseWindow.ToggleShowing()
SendDebug(L"AuctionHouseWindow.ToggleShowing")

    if ( AuctionHouseWindow.IsShowing() == true ) then
        AuctionHouseWindow.Hide()
    else
        AuctionHouseWindow.Show()
    end
end

function AuctionHouseWindow.IsShowing()
SendDebug(L"AuctionHouseWindow.IsShowing")
    return WindowGetShowing( windowName )
end

function AuctionHouseWindow.Clear()
SendDebug(L"AuctionHouseWindow.Clear")

end

function AuctionHouseWindow.OnShown()
SendDebug(L"AuctionHouseWindow.OnShown", 7)

    WindowUtils.OnShown(AuctionHouseWindow.Hide, WindowUtils.Cascade.MODE_NONE)
    VersatileFrame.ResizeWindow( windowName )
end

function AuctionHouseWindow.OnHidden()
SendDebug(L"AuctionHouseWindow.OnHidden", 7)

    WindowUtils.OnHidden()
end


function AuctionHouseWindow.SetTabShowing( tabNumber, isShowing )

	local tabName = AuctionHouseWindow.tabWindowNames[tabNumber]
	ButtonSetPressedFlag( tabName, isShowing )
	ButtonSetStayDownFlag( tabName, isShowing )	
end


function AuctionHouseWindow.OnTabSelected()
SendDebug(L"AuctionHouseWindow.OnTabSelected", 7)

	local tabNumber = WindowGetId( SystemData.ActiveWindow.name  )
	local previousTabNumber = AuctionListDataManager.GetCurrentTabNumber()
	
	-- returns false if pressed same tab
	if AuctionListDataManager.SetCurrentTabNumber( tabNumber ) then
	
		AuctionHouseWindow.SetTabShowing( previousTabNumber, false )
		AuctionHouseWindow.SetTabShowing( tabNumber, true )
		
		AuctionListWindow.SelectTab( tabNumber )
	end
	
end

-- This will minimize/unminimize each section so that we could make different tabs show different sections
-- NOT USED right now
function AuctionHouseWindow.SetSectionShowing( sectionName, isMinimized )

	local windowSettings = AuctionHouseWindow.windowSettings[sectionName]
	windowSettings.isMinimized = isMinimized
	VersatileFrame.setWindowSettings( sectionName, windowSettings )
end

function AuctionHouseWindow.PutInventoryItemUpForAuction( slot, itemData )
	CreateAuctionWindow.PutInventoryItemUpForAuction( slot, itemData )
end


function AuctionHouseWindow.LoadSettings()
SendDebug(L"AuctionHouseWindow.LoadSettings", 7)

	WindowUtils.LoadSettings(windowName, AuctionHouseWindow.versionNumber)

	-- set any changes to AuctionHouseWindow.windowSettings that we presrve the changes for between sessions
	for settingsWindowName, settingsData in pairs(AuctionHouseWindow.savedSettings) do
	
		if AuctionHouseWindow.windowSettings[settingsWindowName] ~= nil then
			AuctionHouseWindow.windowSettings[settingsWindowName].isMinimized = settingsData.isMinimized
			--AuctionHouseWindow.windowSettings[settingsWindowName].isClosed = settingsData.isClosed
		--SendDebug(StringToWString("   "..settingsWindowName.." isMinimized= "..tostring(settingsData.isMinimized)..", isClosed= "..tostring(settingsData.isMinimized)), 7)
		SendDebug(StringToWString("   "..settingsWindowName.." isMinimized= "..tostring(settingsData.isMinimized) ), 7 )
		end
	end
end

function AuctionHouseWindow.SaveSettings()
SendDebug(L"AuctionHouseWindow.SaveSettings", 7)
	 
	-- save any portion of AuctionHouseWindow.windowSettings that may change
	for settingsWindowName, settingsData in pairs(AuctionHouseWindow.windowSettings) do
	
		AuctionHouseWindow.savedSettings[settingsWindowName] = {}
		AuctionHouseWindow.savedSettings[settingsWindowName].isMinimized = settingsData.isMinimized
		--AuctionHouseWindow.savedSettings[settingsWindowName].isClosed = settingsData.isClosed
		--SendDebug(StringToWString("   "..settingsWindowName.." isMinimized= "..tostring(settingsData.isMinimized)..", isClosed= "..tostring(settingsData.isMinimized)), 7)
		SendDebug( StringToWString( "   "..settingsWindowName.." isMinimized= "..tostring(settingsData.isMinimized) ), 7 )
	end
	
    WindowUtils.SaveSettings(windowName)
    
end

function AuctionHouseWindow.DisplayUnrecoverableError( text )

	DialogManager.MakeOneButtonDialog( text, AuctionHouseWindow.okayButtonLabel, AuctionHouseWindow.Hide )
end



--[[
function AuctionHouseWindow.Resize()
SendDebug(L"AuctionHouseWindow.Resize")


end

	local width, height = WindowGetDimensions(windowName.."TitleBar")
	height = height + 20 -- bottom frame and offset from topmost title bar
	
	local lastVisibleWindow = windowName.."ChildWindowsAnchor"
	for name, settings in pairs(AuctionHouseWindow.openedWindows) do
	
		local x, y = WindowGetDimensions(name)
		height = height + y
		
		local windowSettings = VersatileFrame.getWindowSettings( name )
		if WindowGetShowing(name) == true then
			local x, y = WindowGetDimensions(name)
			height = height + y
		end
	end
	WindowSetDimensions(windowName, width, height)
end
--]]

--[[
function AuctionHouseWindow.Resize()
SendDebug(L"AuctionHouseWindow.Resize")

	local width, height = WindowGetDimensions(windowName.."TitleBar")
	height = height + 20 -- bottom frame and offset from topmost title bar
	
	local lastVisibleWindow = windowName.."ChildWindowsAnchor"
	for name, settings in pairs(AuctionHouseWindow.openedWindows) do
		if 
		
			local x, y = WindowGetDimensions(windowName.."TitleBar")
			height = height + y
			lastVisibleWindow = name
	end
	
	WindowSetDimensions(windowName, width, height)
end
--]]
